package com.xinjian.jianba.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.BuziCircleDto;
import com.xinjian.jianba.dto.BuziNameSearchDto;
import com.xinjian.jianba.dto.BuziNameSearchVo;
import com.xinjian.jianba.dto.BuziStoreIndexDto;
import com.xinjian.jianba.dto.BuziStoreIndexVo;
import com.xinjian.jianba.dto.BuziWorkVo;
import com.xinjian.jianba.dto.CollectBuziDto;
import com.xinjian.jianba.dto.CollectListDto;
import com.xinjian.jianba.dto.CollectListVo;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.BuziVisitService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;
@RestController
@RequestMapping("/api/v3/jianba/business")
public class BuziInfoBusinessController extends AbstractController{

	@Autowired
	private BuziVisitService buziVisitService;
	@Autowired
	private ValidatorService validatorService;
	private static final Logger LOGGER = LoggerFactory.getLogger(BuziInfoBusinessController.class);
	/**
	 * 根据店铺名称搜索
	 * @param buziNameSearchDto
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/buziNameSearch")
	public String buziNameSearch(@RequestBody BuziNameSearchDto buziNameSearchDto) throws IOException{
		try {
			validatorService.valid(buziNameSearchDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.ERROR_MSG);
		}
		List<BuziNameSearchVo> list=buziVisitService.buziNameSearch(buziNameSearchDto);
		if(CollectionUtils.isEmpty(list)){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, new ArrayList<BuziNameSearchVo>());
		}else{
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, list);
		}
	}
	/**
	 * 店铺首页查询
	 * @param buziStoreIndexDto
	 * @return
	 */
	@PostMapping("/buziStoreIndex")
	public String buziStoreIndex(@RequestBody BuziStoreIndexDto buziStoreIndexDto){
		try {
			validatorService.valid(buziStoreIndexDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.ERROR_MSG);
		}
		BuziStoreIndexVo buziStoreIndexVo=buziVisitService.buziStoreIndex(buziStoreIndexDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, buziStoreIndexVo);
	}
	
	/**
	 * 商圈搜索
	 * @param buziCircleDto
	 * @return
	 */
	@PostMapping("/buziCircle")
	public String buziCircle(@RequestBody BuziCircleDto buziCircleDto){
		try {
			validatorService.valid(buziCircleDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.ERROR_MSG);
		}
		List<BuziNameSearchVo> list=buziVisitService.buziCircle(buziCircleDto);
		if(CollectionUtils.isEmpty(list)){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, new ArrayList<BuziNameSearchVo>());
		}else{
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, list);
		}
	}
	@PostMapping("/buziDetail")
	public String buziDetail(@RequestBody Map<String, String> map){
		String workId=map.get("workId");
		if(StringUtils.isEmpty(workId)){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, "活动id不能为空");
		}
		BuziWorkVo buziWorkVo=buziVisitService.findWorkDetai(Long.parseLong(workId));
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, buziWorkVo);
	}
	/**
	 * 店铺收藏
	 */
	@PostMapping("/collectBuzi")
	public String collectBuzi(@RequestBody CollectBuziDto collectBuziDto){
		LOGGER.info("店铺收藏--------{}",JsonUtil.getJsonString(collectBuziDto));
		try {
			validatorService.valid(collectBuziDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.ERROR_MSG);
		}
		Long collectId=buziVisitService.collectBuzi(collectBuziDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, collectId);
	}
	/**
	 * 取消店铺收藏
	 */
	@PostMapping("/cancleCollectBuzi")
	public String cancleCollectBuzi(@RequestBody Map<String, String> map){
		LOGGER.info("取消店铺收藏--------{}",JsonUtil.getJsonString(map));
		String collectId=map.get("collectId");
		if(StringUtils.isBlank(collectId)){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.ERROR_MSG);
		}
		buziVisitService.cancleCollectBuzi(collectId);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
	}
	
	/**
	 * 查看收藏的店铺
	 */
	@PostMapping("/collectList")
	public String buziVisitService(@RequestBody CollectListDto collectListDto){
		LOGGER.info("查看收藏的店铺--------{}",JsonUtil.getJsonString(collectListDto));
		List<CollectListVo> list=buziVisitService.buziVisitService(collectListDto); 
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,list);
	}
}
